package algorithm.array;

import java.util.Arrays;

class TreeNode{
    int value;
    TreeNode left;
    TreeNode right;

    public TreeNode() {}

    public TreeNode(int value) {this.value = value;}

    public TreeNode(int value, TreeNode left, TreeNode right) {
        this.value = value;
        this.left = left;
        this.right = right;
    }
}

public class SortArrayToBST {
    public static void main(String[] args) {
        int [] arr = {1,2,3,4,5,6,3,4,5,1,2,2,1};
        //sortedArrayToBST(arr);

        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static TreeNode sortedArrayToBST(int[] nums) {
        int n = nums.length;
        if(n==0) return null;
        int m = (n-1)/2;
        TreeNode rootNode = new TreeNode(nums[m]);
        rootNode.left = sortedArrayToBST(Arrays.copyOfRange(nums,0,m));
        rootNode.right = sortedArrayToBST(Arrays.copyOfRange(nums,m+1,n));


        return rootNode;
    }
}
